import { getYMD, getNowFormatDate, getDay, compareObject } from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            title: 'CPS概况',
            xlsxTitle: "",
            xlsxTitleQ: "",
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            nowDate: getNowFormatDate(),
            date: getYMD(0),
            playType: 'all',
            loading: false,
            searchable: false,
            activeIndex: 'all',
            pickerOptions: {
                disabledDate(time) {
                    if (time.getTime() > Date.now()) {
                        return time.getTime();
                    }
                }
            },
            dialogFormVisible: false,
            formLabelWidth: '100px',
            tabelData: [],
            tabelDatas: [],
            /*realTimeServiceContent 的宽*/
            realTimeServiceContent: '100%',
            /*表的宽度*/
            realTimeServiceTableWidth: "100%",
            /*数据表头*/
            tabelHead: [
                { prop: "channelid", label: "渠道名称", width: "140" },
                { prop: "activate", label: "激活设备", width: "140" },
                { prop: "newUser", label: "新增帐号", width: "140" },
                { prop: "activeUser", label: "活跃帐号", width: "140" },
                { prop: "payUser", label: "付费帐号", width: "140" },
                { prop: "pay", label: "付费金额(" + localStorage.getItem("ZULONGBICURRENCY") + ")", width: "140" },
                { prop: "payUserInActiveRadio", label: "活跃付费率", width: "140" },
                { prop: "payARPU", label: "付费ARPU(" + localStorage.getItem("ZULONGBICURRENCY") + ")", width: "160" },
                { prop: "activeARPU", label: "活跃ARPU(" + localStorage.getItem("ZULONGBICURRENCY") + ")", width: "160" },
                { prop: "the2LeftRadio", label: `${getDay(-1)}-次日留存率`, width: "170" },
                { prop: "the3LeftRadio", label: `${getDay(-2)}-3日留存率`, width: "170" },
                { prop: "the7LeftRadio", label: `${getDay(-6)}-7日留存率`, width: "170" }
            ],
            /*当前页显示数量*/
            pageSize: 20,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
        }
    },
    computed: {
        // xlsx() {
        //     return this.title + '.xlsx'
        // }
    },
    methods: {
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        // 分页器
        // 控制每页的数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.tableControl();
        },
        // 页数
        handleCurrentChange(val) {
            this.currentPage = val;
            this.tableControl();
        },
        tableControl() {
            this.tabelData = [];
            this.tabelData = this.tabelDatas.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
        },
        dateChange(val) {
            this.date = val
            console.log(this.date)
        },
        loads() {
            this.loading = !this.loading; //加载中
            this.searchable = !this.searchable; //查询按钮不可用
        },
        //查询按钮
        search(form) {
            if (this.date == undefined || this.date.length == 0) {
                this.errorFun('请完善输入的值！');
                this.date = getNowFormatDate()
                return false;
            } else {
                this.getData('all')
            }
        },
        // 表格下载
        excel(tableIndex, sheet) {
            var that = this;

            that.xlsxTitle = sheet + "-" + that.xlsxTitleQ + "-" + that.date;
            that.downloadExl(that.tabelHead, that.tabelDatas, that.xlsxTitle, that.title);

        },
        /*获取数据*/
        getData(playType) {
            this.loads()
            this.activeIndex = playType;
            if (playType == "all") {
                this.xlsxTitleQ = "全平台";
            } else if (playType == "2") {
                this.xlsxTitleQ = "iOS";
            } else if (playType == "3") {
                this.xlsxTitleQ = "安卓";
            }
            // `${this.SRC}realTime/platformAndChannelRealTime/searchCps.do?date=${this.date}&selectedPlatform=${playType}`
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=searchCps`, {
                params: { 'ps': `date=${this.date}&selectedPlatform=${playType}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body))
                this.loads()
                if (parseInt(data['res']) === 0) {
                    this.nowDate = getNowFormatDate()
                    this.dataTotal = data.data.length;
                    if (data.data.length == 0) {
                        this.tabelData = [];
                        this.errorFun("暂无数据！")
                    } else {
                        var arr = data.data
                        for (var i = 0, len = arr.length; i < len; i++) {
                            arr[i].payUserInActiveRadio = this.dataFormat(arr[i].payUserInActiveRadio)
                            arr[i].the2LeftRadio = this.dataFormat(arr[i].the2LeftRadio)
                            arr[i].the3LeftRadio = this.dataFormat(arr[i].the3LeftRadio)
                            arr[i].the7LeftRadio = this.dataFormat(arr[i].the7LeftRadio)
                        }
                        this.tabelDatas = [];
                        this.tabelDatas = arr;
                        this.tabelDatas.sort(compareObject('activeUser', false));
                        this.tableControl();
                    }
                } else {
                    this.tabelDatas = [];
                    this.errorFun(data['msg'])
                }
            })
        },
        //小数变成百分数
        dataFormat(val) {
            var temp = val * 100
            val = temp.toString().substring(0, 5) + '%'
            return val;
        },
        /*设置初始值*/
        _init() {
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.realTimeServiceTableWidth = document.body.clientWidth - 240 + "px";
                this.realTimeServiceContent = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // 表格排序
        sortChange(column) {
            if (column.prop === null) {
                return;
            } else if (column.order === 'descending') {
                this.tabelDatas.sort(compareObject(column.prop, false));
            } else { //正序
                this.tabelDatas.sort(compareObject(column.prop, true));
            }
            this.tableControl();
        },
    },
    mounted() {
        this._init()
            // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
        console.log(this.DP.uid)
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        this.getData(this.playType)
    }
}